import heapq

n = int(input())

heap = []
for i in input().split():
    heapq.heappush(heap, int(i))

res = 0
while len(heap) > 1:
    a = heapq.heappop(heap)
    b = heapq.heappop(heap)
    res += a + b
    heapq.heappush(heap, a + b)

print(res)
